from django.shortcuts import render
from django.http import HttpResponse
from .TypesViews import Gettypesall
from .IndexViews import uploads
from ..models import Types,Goods
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import permission_required
# 添加页面
@permission_required('myadmin.insert_goods',raise_exception = True)
def goods_add(request):
    # 获取分类数据
    context = {'typesslist':Gettypesall()}

    return render(request,'admin/goods/add.html',context)

# 执行添加
@permission_required('myadmin.insert_goods',raise_exception = True)
def goods_insert(request):
    try:
        # 接收数据
        data = request.POST.dict()
        # print(data)
        data.pop('csrfmiddlewaretoken')

        # 上传图片 获取图片
        myfile = request.FILES.get('pic',None)
        # 判断是否没有图片上传
        if not myfile:
            return HttpResponse('<script>alert("请上传商品图片");location.href="'+reverse('admin_goods_add')+'"</script>')

        # 执行图片上传操作
        data['pic'] = uploads(myfile)

        print(Types.objects.get(id=data['tid']))

        # 处理分类数据  注意 因为是一对多模型关系  tid 必须是一个对象
        data['tid'] = Types.objects.get(id=data['tid'])

        # 上传数据库
        Goods.objects.create(**data)
        return render(request,'info.html',{'msg':'添加成功','url':reverse('admin_goods_list')})
    except:
        return render(request, 'info.html', {'msg': '添加失败', 'url': reverse('admin_goods_add')})
# 列表页
@permission_required('myadmin.show_goods',raise_exception = True)
def goods_list(request):

    # 获取所有的商品数据 除了状态为3（下架）的
    goodslist = Goods.objects.exclude(status=3)

#搜索
    # 获取搜索条件
    keywords = request.GET.get('keywords', None)
    # # 判断是否有搜索条件
    if keywords:
        # 进行搜索
        goodslist = goodslist.filter(title__contains = keywords)
        # 凭借搜索条件

# 分页
    # 导入分页模块
    from django.core.paginator import Paginator
    # 实例化参数对象  参数1：数据列表  参数2 每页显示的条数
    p = Paginator(goodslist,10)
    # 获取当前页码数 有传显示页码数  没有显示1
    page = request.GET.get('page',1)
    # 获取当前页数的数据
    goodslist = p.page(page)

    # 分配数据
    context = {'goodslist':goodslist}
    # 加载模板
    return render(request,'admin/goods/list.html',context)

# 删除
@permission_required('myadmin.del_goods',raise_exception = True)
def goods_del(request):
    try:
        # 获取id
        id = request.GET['uid']
        # print(id)
        # 获取数据对象
        ob = Goods.objects.get(id=id)
        ob.status = 3
        ob.save()
        return render(request, 'info.html', {'msg': '删除成功', 'url': reverse('admin_goods_list')})
    except:
        return render(request, 'info.html', {'msg': '删除失败', 'url': reverse('admin_goods_list')})

# 显示修改页面
@permission_required('myadmin.edit_goods',raise_exception = True)
def goods_uppage(request):
#     获取用户id
    id = request.GET['uid']
    # 获取数据
    data = Goods.objects.get(id=id)
    # 分配数据
    context = {'data': data}
    # 加载模板
    return render(request, 'admin/goods/uppage.html', context)

# 执行修改
@permission_required('myadmin.edit_goods',raise_exception = True)
def goods_upinsert(request):

    try:
        # 获取数据对象
        ob = Goods.objects.get(id=request.POST['uid'])
        ob.title = request.POST['title']
        ob.price = request.POST['price']
        ob.num = request.POST['num']
        ob.status = request.POST['status']
        ob.info = request.POST['info']
        # 接收图片信息
        myfile = request.FILES.get('pic',None)
        # 判断是否有图片上传
        if  myfile:
            ob.pic = uploads(myfile)
        ob.save()
        return render(request, 'info.html', {'msg': '修改加成功', 'url': reverse('admin_goods_list')})
    except:
        return render(request, 'info.html', {'msg': '修改失败', 'url': reverse('admin_goods_uppage')})